<?php
/**
 * 固定传播结算
 * @author qianxun
 *
 */
class SpreadSettle extends MY_Controller{
    function SpreadSettle(){
        parent::MY_Controller();
        $this->navbarFocus = 'SpreadBudget';
    }
    /**
     * 专营店列表
     */
    function storeList(){
        if ($this->user['userRole'] != 1){
            showError('您无权限进去此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'storeList'),'title'=>'发布与核检结算 ');
        //载入模型
        $this->load->model('spreadProcessModel');
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $where = array();
        //专营店ID条件
        $where['storeId'] = array('in', $this->userLib->getViewStoreIds());
        //年份条件
        $where['year'] = $year;
        //固定传播基金金额汇总
        $procList = $this->spreadProcessModel->getNewList($where);
        foreach ($procList as $k => $v){
            if ($v['id'] != $v['belongProcId']){
                $belogProcData = $this->spreadProcessModel->getNewData($v['belongProcId']);
                $v['auditRoleStr'] = $belogProcData['auditRoleStr'];
                $v['auditRsStr'] = $belogProcData['auditRsStr'];
            }
            $procList[$k] = $v;
        }
        $this->viewData['procList'] = $procList;
        
        $this->load->view('SpreadSettle/storeList', $this->viewData);
    }
    /**
     * 专营店发布提交
     */
    function storeEdit(){
        if ($_POST){
            $procId = $this->input->post('procId');
            $procId || showError('请选择要提交的主线');
            $_POST['voucher'] || showError('暂无双月检核发布');
            $this->load->model('fileModel');
            $voucher = array();
            foreach ($_POST['voucher'] as $k => $v){
                isset($voucher[$k]) || $voucher[$k] = array();
                $voucher[$k] = $this->fileModel->getList(array('id'=>array('in', $v)), NULL, NULL, 'id,fileName,origName,viewPath');
            }
            $voucher || showError('请上传自证照片');
            foreach ($voucher as $v){
                ((array)$v && count((array)$v) > 0 && count((array)$v) <= 5) || showError('请上传符合条件数量的自证照片');
            }
            $this->load->model('spreadProItemModel');
            foreach ($voucher as $k => $v){
                $data = array(
                    'voucher' => serialize((array)$v),
                    'auditRs' => 0,
                );
                if ((array)$v && $data['voucher'] != serialize(array())) {
                    $this->spreadProItemModel->edit(array('id'=>$k,'storeId'=>$this->user['userId']), $data);
                    $this->spreadProItemModel->updateMoney($k);
                }else{
                    showError('请上传符合条件数量的自证照片');
                }
            }
            showSuccess('发布成功', printUrl('SpreadSettle', 'storeList'));
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择要查看的固定传播基金申请');
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'storeList'),'title'=>'固定传播基金发布与检核列表 ');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'固定传播基金双月检核发布');
            //项目数据
            $this->load->model('spreadProcessModel');
            $procData = $this->spreadProcessModel->getNewData($procId);
            if (!$this->auth->isSpreadSettleEdit($procData)){
                showError('您无权发布');
            }
            $this->viewData['procData'] = $procData;
            //细项列表
            $this->load->model('spreadProjectModel');
            $projectList = $this->spreadProjectModel->getWaitSettleAuditList($procId);
            $this->viewData['projectList'] = $projectList;
            //var_dump($projectList);exit();
            //专营店数据
            $this->load->model('sellpointModel');
            $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);;
            //操作
            $this->viewData['action'] = 'edit';
            
            $this->load->view('SpreadSettle/storeEdit', $this->viewData);
        }
    }
    /**
     * 管理列表
     */
    function adminList(){
        if ($this->user['userRole'] == 1){
            showError('您无权限进去此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'固定传播基金管理');
        //载入模型
        $this->load->model('spreadProcessModel');
        //$this->load->model('spreadProjectModel');
        $this->load->model('spreadProItemModel');
        $this->load->model('sellpointModel');
        //查询条件
        $where = array();
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $spsarea = $this->input->get('spsarea');
        $this->viewData['spsarea'] = $spsarea;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = (int)$this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, $spsarea, $province, $city, $storeId, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $where['storeId'] = array('in', $placeStoreIds);
        }else{
            $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //年份条件
        $where['year'] = $year;
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] && $where['timeUnit'] = $this->viewData['timeUnit'];
        //$where['budgetApply > '] =0;
        //审核通过
        $where['auditRs >= '] = 0;
        $count = $this->spreadProItemModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl($_GET['c'], $_GET['m'], $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        
        $itemList = $this->spreadProItemModel->getNewList($where, $limit, $page, '', 'id desc');
        $procIds = array();
        $projectIds = array();
        foreach ($itemList as $v){
            in_array($v['procId'], $procIds) || $procIds[] = $v['procId'];
            in_array($v['projectId'], $projectIds) || $projectIds[] = $v['projectId'];
        }
        $this->viewData['itemList'] = $itemList;
        $procList = $this->spreadProcessModel->getNewList(array('id'=>array('in',$procIds)));
        foreach ($procList as $k => $v){
            if ($v['id'] != $v['belongProcId']){
                $belogProcData = $this->spreadProcessModel->getNewData($v['belongProcId']);
                $v['auditRoleStr'] = $belogProcData['auditRoleStr'];
                $v['auditRsStr'] = $belogProcData['auditRsStr'];
            }
            $procList[$k] = $v;
        }
        $this->viewData['procList'] = $procList;
        //$this->viewData['projectList'] = $this->spreadProjectModel->getNewList(array('id'=>array('in',$projectIds)));
        $this->load->view('SpreadSettle/adminList', $this->viewData);
    }
    /**
     * 发布审核
     */
    function adminAudit(){
        if ($_POST){
            $procId = (int)$this->input->post('procId');
            $procId || showError('请选择要审核的主线流程');
            $audit = $this->input->post('audit');
            $audit || showError('请选择审核项');
            //校验审核结果
            foreach ($audit as $k => $v){
                (int)$v['auditRs'] || showError('审核结果选项非法');
            }
            //提交类型
            $subtype = (int)$this->input->post('subtype');
            $this->load->model('spreadProItemModel');
            foreach ($audit as $k => $v){
                $data = array(
                    'auditRole' => $this->user['userRole'],
                    'auditRs' => (int)$v['auditRs'],
                    'auditRemark' => (string)$v['auditRemark'],
                    'auditTime' => time(),
                );
                $this->spreadProItemModel->edit($k, $data);
                $this->spreadProItemModel->updateMoney($k);
            }
            $jumpUrl = printUrl('SpreadSettle','adminList');
            if ($subtype == 1){//如果继续审核下一家专营店
                //审核下一个专营店
                $where = array('auditRs' => 0);
                //权限内专营店列表
                $viewStoreIds = $this->userLib->getViewStoreIds();
                if ($viewStoreIds == '*'){
                
                }else{
                    $where['storeId'] = array('in', $viewStoreIds);
                }
                $nextProItemData = $this->spreadProItemModel->getList($where, 1, 0, '', 'id desc');
                $nextProItemData = array_values($nextProItemData);
                if ($nextProItemData){
                    $jumpUrl = printUrl('SpreadSettle', 'adminAudit', array('itemId'=>$nextProItemData[0]['id']));
                }
            }
            showSuccess('提交审核成功', $jumpUrl);
        }else{
            $itemId = (int)$this->input->get('itemId');
            $itemId || showError('请选择要审核的固定传播基金申请');
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'adminList'),'title'=>'固定传播基金管理列表');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'固定传播基金申请查看');
            //载入模型
            $this->load->model('spreadProcessModel');
            $this->load->model('spreadProcStoreModel');
            $this->load->model('spreadProItemModel');
            $this->load->model('spreadProjectModel');
            $this->load->model('timeUnitModel');
            //细项数据
            $itemData = $this->spreadProItemModel->getNewData(array('id' => $itemId, 'auditRs >=' => 0) );
            $itemData || showError('该明细项无法审核');
            if (!$this->auth->isSpreadSettleAudit($itemData)){
                showError('您无权审核', printUrl('SpreadSettle', 'adminList'));
            }
            $this->viewData['itemData'] = $itemData;
            //细项列表
            $itemList = $this->spreadProItemModel->getList(array('procId'=>$itemData['procId'],'timeUnit'=>$itemData['timeUnit'], 'auditRs >=' => 0));
            $this->viewData['itemList'] = $itemList;
            //项目列表
            $projectIds = array();
            foreach ($itemList as $v){
                in_array($v['projectId'], $projectIds) || $projectIds[] = $v['projectId'];
            }
            $projectList = $this->spreadProjectModel->getNewList(array('id'=>array('in',$projectIds)));
            $this->viewData['projectList'] = $projectList;
            //主线数据
            $procData = $this->spreadProcessModel->getNewData($itemData['procId']);
            $this->viewData['procData'] = $procData;
            //专营店数据
            $this->load->model('sellpointModel');
            $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($itemData['storeId']);;
            //已选专营店列表
            $storeList = $this->spreadProcStoreModel->getList(array('procId'=>$itemData['procId']));
            $this->viewData['storeList'] = $storeList;
            //审核日志
            //$this->load->model('auditLogModel');
            //$this->viewData['auditData'] = $this->auditLogModel->getLastLog(52, $itemData['procId']);
            //操作
            $this->viewData['action'] = 'audit';
            
            $this->load->view('SpreadSettle/adminAudit', $this->viewData);
        }
    }
    /**
     * 结算查看
     */
    function check(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择要查看的固定传播基金申请');
        //面包屑
        if ($this->user['userRole'] == 1){
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'storeList'),'title'=>'发布与核检结算 ');
        }else{
            $this->viewData['breadcrumb'][] = array('url'=>$_SERVER['HTTP_REFERER'],'title'=>'发布与核检结算管理');
        }
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'固定传播基金申请查看');
        
        //载入模型
        $this->load->model('spreadProcessModel');
        $this->load->model('spreadProcStoreModel');
        $this->load->model('spreadProItemModel');
        $this->load->model('timeUnitModel');
        //主线流程数据
        $procData = $this->spreadProcessModel->getNewData($procId);
        if (!$this->auth->isSpreadSettleCheck($procData)){
            showError('您无权限查看');
        }
        $this->viewData['procData'] = $procData;
        //专营店数据
        $this->load->model('sellpointModel');
        $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);
        $procId = $procData['belongProcId'];
        //已选专营店列表
        $storeList = $this->spreadProcStoreModel->getList(array('procId'=>$procId));
        $this->viewData['storeList'] = $storeList;
        //明细项列表
        $itemList = $this->spreadProItemModel->getItemListByTimeUnit($procId);
        $this->viewData['itemList'] = $itemList;
        //时段列表
        $newTimeUnitList = array();
        $timeUnitList = $this->timeUnitModel->getList(array('id > '=>0), NULL, NULL, 'id,year,name', 'orderNum asc');
        foreach ($timeUnitList as $v){
            $newTimeUnitList[$v['id']] = $v;
        }
        $this->viewData['timeUnitList'] = $newTimeUnitList;
        //操作
        $this->viewData['action'] = 'check';
        
        $this->load->view('SpreadSettle/check', $this->viewData);
    }
    /**
     * 返回指定投放类型相册
     */
    function storeItemPhotos(){
        $itemId = (int)$this->input->get('itemId');
        $data = array(
            'title' => '会签审批表查看',
            'id' => $itemId,
            'start' => 0,
            'data' => array(),
        );
        if ($itemId){
            $this->load->model('spreadProItemModel');
            $itemData = $this->spreadProItemModel->getNewData($itemId);
            if ($itemData['voucher']){
                foreach ((array)$itemData['voucher'] as $v){
                    $data['data'][] = array(
                        'alert' => $v['origName'],
                        'pid' => $v['id'],
                        'src' => $v['viewPath'],
                        'thumb' => $v['viewPath'],
                    );
                }
            }
        }
    
        echo jsonEncode($data);
    }
    /**
     * 申报审核时间管理
     */
    function applyTimeList(){
        $this->load->model('configModel');
        $this->viewData['spreadApplyTime'] = $this->configModel->getConfigData('spreadApplyTime');
        //当前菜单
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'applyTimeList'),'title'=>'申报审核时间管理');
        
        $this->load->view('SpreadSettle/applyTimeList', $this->viewData);
    }
    /**
     * 修改全网起止时间
     */
    function applyTimeEdit(){
        $this->load->model('configModel');
        if ($_POST){
            $data = array(
                'startTime' => $this->input->post('startTime'),
                'endTime' => $this->input->post('endTime'),
            );
            $data['startTime'] || showError('请选择开始时间');
            $data['startTime'] = strtotime($data['startTime']);
            $data['endTime'] || showError('请选择结束时间');
            $data['endTime'] = strtotime($data['endTime']);
            $data['startTime'] < $data['endTime'] || showError('开始时间必须早于结束时间');
            $this->configModel->saveConfigData('spreadApplyTime', $data);
            showSuccess('修改成功');
        }else{
            $this->viewData['spreadApplyTime'] = $this->configModel->getConfigData('spreadApplyTime');
            $this->load->view('SpreadSettle/applyTimeEdit', $this->viewData);
        }
    }
    /**
     * 补报权限列表
     * @param int $fundType
     * @param int $level
     */
    function roleList() {
        if ($this->user['userRole'] == 1){
            showError('您无权限进去此页面');
        }
        //载入模型
        $this->load->model('supplementRoleModel');
        $this->load->model('sellpointModel');
        //查询条件
        $where = array();
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //补报类型
        $type = (int)$this->input->get('type');
        in_array($type, array(1, 2)) || $type = 2;
        $this->viewData['type'] = $type;
        //基金类型
        $fundType = 5;
        $this->viewData['fundType'] = $fundType;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, '', $province, $city, $storeId, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        $where['storeId'] = array('in', $placeStoreIds);
        //权限内专营店列表
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表,地区端只能查看自己添加的列表
        if ($this->user['userRole'] == $this->userLib->roleArea){
            $viewStoreIds = $this->userLib->getViewStoreIds();
            if ($viewStoreIds == '*'){
                $where['storeId'] = array('in', $placeStoreIds);
            }else{
                $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
            }
        }
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $timeUnit = 0;
        $where['timeUnit'] = $timeUnit;
        //年份条件
        $where['year'] = $year;
        $fundType && $where['fundType'] = $fundType;
        $type && $where['type'] = $type;
        $userRole = $this->user['userRole'];
        if ($userRole == 5){//区域总监
            $where['userRole'] = $userRole;
        }elseif ($userRole == 6){
    
        }
        $count = $this->supplementRoleModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['page']);
        $config['base_url'] = printUrl($getParam['c'], $getParam['m'], $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
    
        $this->viewData['supplementList'] = $this->supplementRoleModel->getNewList($where, $limit, $page, '', 'id desc');
        //当前菜单
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'applyTimeList'),'title'=>'申报审核时间管理');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'roleList'),'title'=>'单店特殊开通列表');
        //选中子菜单
        $this->navChildFocus = 'SpreadSettle_applyTimeList';
        $this->load->view('SpreadSettle/roleList', $this->viewData);
    }
    /**
     * 补报通道开通列表
     */
    function roleAdd(){
        $this->load->model('timeUnitModel');
        if ($_POST){
            $where = array(
                'year'      => (int)$this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
                'type'     => (int)$this->input->post('type'),
                'fundType'  => (int)$this->input->post('fundType'),
            );
            $data = array(
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'userRole'  => $this->user['userRole'],
                'userId'    => $this->user['userId'],
                'isDel'     => 0,
                'createTime'=> time()
            );
            $where['year'] || showError('请选择年份');
            //$where['timeUnit'] || showError('请选择双月');
            $where['type'] || showError('请选择补报类型');
            $where['fundType'] || showError('请选择基金类型');
            //补报时间
            $data['startTime'] || showError('请选择补报开始时间');
            $data['startTime'] = strtotime($data['startTime']);
            $data['endTime'] || showError('请选择补报结束时间');
            $data['endTime'] = strtotime($data['endTime']);
            $data['startTime'] < $data['endTime'] || showError('开始时间必须小于结束时间');
            
            $selectedStoreList = $this->input->post('selectedStoreList');
            $selectedStoreList || showError('请选择要开通补报的专营店');
            $this->load->model('supplementRoleModel');
            foreach ((array)$selectedStoreList as $v){
                $where['storeId'] = (int)$v;
                if ($where['storeId']){
                    $this->supplementRoleModel->upset($where, $data);
                }
            }
            showSuccess('添加成功', printUrl('SpreadSettle', 'roleList'));
        }else{
            if(!$this->auth->isSupplementEdit()){
                showError('您没有补报申报权限');
            }
            //补报类型
            $type = 2;
            in_array($type, array(1,2)) || showError('请选择补报类型');
            $this->viewData['type'] = $type;
            //当前菜单
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'applyTimeList'),'title'=>'申报审核时间管理');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'roleList'),'title'=>'单店特殊开通列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'roleAdd'),'title'=>'单店特殊开通');
            //年份
            $year = $this->input->get('year');
            $year || $year = $this->config->item('nowYear');
            $this->viewData['year'] = $year;
            //时段
            $timeUnit = (int)$this->input->get('timeUnit');
            $this->viewData['timeUnit'] = $timeUnit;
            //基金类型
            $this->viewData['fundType'] = 5;
            //选中子菜单
            $this->navChildFocus = 'SpreadSettle_applyTimeList';
            $this->load->view('SpreadSettle/roleAdd', $this->viewData);
        }
    }
    /**
     * 补报通道修改
     */
    function roleEdit(){
        $this->load->model('supplementRoleModel');
        $this->load->model('sellpointModel');
        $this->load->model('timeUnitModel');
        if ($_POST){
            $where = array(
                'id'        => (int)$this->input->post('id'),
                'year'      => (int)$this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
                'type'     => (int)$this->input->post('type'),
                'fundType'  => (int)$this->input->post('fundType'),
            );
            $data = array(
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'userRole'  => $this->user['userRole'],
                'userId'    => $this->user['userId'],
                'isDel'     => 0
            );
            $where['id'] || showError('请选择要修改的补报数据');
            //补报时间
            $data['startTime'] || showError('请选择补报开始时间');
            $data['startTime'] = strtotime($data['startTime']);
            $data['endTime'] || showError('请选择补报结束时间');
            $data['endTime'] = strtotime($data['endTime']);
            $data['startTime'] < $data['endTime'] || showError('开始时间必须小于结束时间');
            if ($this->supplementRoleModel->upset($where, $data)){
                showSuccess('修改补报成功');
            }else{
                showError('修改补报失败');
            }
        }else{
            $id = $this->input->get('id');
            $id || showError('请选择要修改的补报权限');
            $supplementData = $this->supplementRoleModel->getNewData($id);
            $supplementData || showError('数据不存在');
            $this->viewData['supplementData'] = $supplementData;
            $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($supplementData['storeId']);
            $this->load->view('SpreadSettle/roleEdit', $this->viewData);
        }
    }
    /**
     * 时段列表
     */
    function timeUnitList(){
        //年份
        $this->viewData['yearList'] = $this->timeUnitModel->getCacheData('yearList');
        $where = array('id >'=>0);
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getNewList($where, '', '', '', 'orderNum asc,id asc');
        //echo json_encode($this->viewData['timeUnitList']);exit;
        //当前菜单
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('SpreadSettle', 'timeUnitList'),'title'=>'双月检核时间管理');
        
        $this->load->view('SpreadSettle/timeUnitList', $this->viewData);
    }
    /**
     * 增加或修改时段
     */
    function timeUnitAdd(){
        $this->theme = 'bootstrap';
        if ($_POST){
            $id = $this->input->post('id');
            $data = array(
                'year'  => (int)$this->input->post('year'),
                'name'  => $this->input->post('name'),
                'orderNum'  => (int)$this->input->post('orderNum'),
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
            );
            //固定传播基金
            $spreadTimePost = $this->input->post('spreadTime');
            $spreadTimeSetting = array(
                'budgetApplyStart'      => $spreadTimePost['budgetApplyStart'],
                'budgetApplyEnd'        => $spreadTimePost['budgetApplyEnd'],
                'budgetAuditStart'      => $spreadTimePost['budgetAuditStart'],
                'budgetAuditEnd'        => $spreadTimePost['budgetAuditEnd'],
                'settleApplyStart'      => $spreadTimePost['settleApplyStart'],
                'settleApplyEnd'        => $spreadTimePost['settleApplyEnd'],
                'settleAuditStart'      => $spreadTimePost['settleAuditStart'],
                'settleAuditEnd'        => $spreadTimePost['settleAuditEnd'],
                'settleChangeStart'     => $spreadTimePost['settleChangeStart'],
                'settleChangeEnd'       => $spreadTimePost['settleChangeEnd'],
            );
            $data['year'] || showError('请输入年份');
            $data['year'] >= 2016 || showError('年份应大于或等于2016年');
            $data['name'] || showError('请输入时段名称');
            $data['orderNum'] || $data['orderNum'] = 0;
            $data['startTime'] || showError('请选择时段开始时间');
            $data['endTime'] || showError('请选择时段结束时间');
            date('d', strtotime($data['startTime'])) == 1 || showError('开始时间非当月第一天');
            //结束时间戳
            $endTime = strtotime($data['endTime']);
            date('d', $endTime) == date('t', $endTime) || showError('结束时间非当月最后一天');
            if ($this->timeUnitModel->getData(array('year'=>$data['year'],'name'=>$data['name'],'id !='=>$id))){
                showError('已存在同名时段');
            }
            //默认初始化时间
            /**
             * 固定传播基金时间设置
             */
            //固定传播预算申请开始时间默认为【时段开始前十天】
            $spreadTimeSetting['budgetApplyStart'] || $spreadTimeSetting['budgetApplyStart'] = date("Y-m-d H:i:s", strtotime($data['startTime'].' -10 day'));
            //固定传播预算申请结束时间默认为【时段结束时间】
            $spreadTimeSetting['budgetApplyEnd'] || $spreadTimeSetting['budgetApplyEnd'] = $data['endTime'];
            //固定传播预算审核开始时间默认为【预算申报开始时间】
            $spreadTimeSetting['budgetAuditStart'] || $spreadTimeSetting['budgetAuditStart'] = $spreadTimeSetting['budgetApplyStart'];
            //固定传播预算审核结束时间默认为【时段结束后15日内】
            $spreadTimeSetting['budgetAuditEnd'] || $spreadTimeSetting['budgetAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +15 day'));
            //固定传播结算申报开始时间默认为【时段开始时间】
            $spreadTimeSetting['settleApplyStart'] || $spreadTimeSetting['settleApplyStart'] = $data['startTime'];
            //固定传播结算申报结束时间默认为【时段结束后10日内】
            $spreadTimeSetting['settleApplyEnd'] || $spreadTimeSetting['settleApplyEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +10 day'));
            //固定传播结算审核开始时间默认为【时段开始时间】
            //$spreadTimeSetting['settleAuditStart'] || $spreadTimeSetting['settleAuditStart'] = $data['startTime'];
            $spreadTimeSetting['settleAuditStart'] = $spreadTimeSetting['settleApplyStart'];
            //固定传播结算审核结束时间默认为【时段结束后20天内】
            //$spreadTimeSetting['settleAuditEnd'] || $spreadTimeSetting['settleAuditEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            $spreadTimeSetting['settleAuditEnd'] = $spreadTimeSetting['settleApplyEnd'];
            //固定传播金额修改开始时间
            $spreadTimeSetting['settleChangeStart'] || $spreadTimeSetting['settleChangeStart'] = $data['startTime'];
            //固定传播金额修改结束时间
            $spreadTimeSetting['settleChangeEnd'] || $spreadTimeSetting['settleChangeEnd'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +30 day'));
            $data['spreadTimeSetting'] = serialize($spreadTimeSetting);
            if ($id){
                $timeId = $this->timeUnitModel->edit($id, $data);
            }else{
                $timeId = $this->timeUnitModel->add($data);
            }
            if ($timeId){
                $this->timeUnitModel->createTimeUnitCache();
                showSuccess('时段保存成功');
            }else{
                showError('时段保存失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            if ($id){
                $this->viewData['timeUnitData'] = $this->timeUnitModel->getNewData($id);
                $this->viewData['timeUnitData'] || showError('数据不存在');
            }
            $this->load->view('SpreadSettle/timeUnitAdd', $this->viewData);
        }
    }
}